package com.guli.mall.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.CopyPageProperties;
import com.guli.mall.wms.entity.WareInfoEntity;
import com.guli.mall.wms.entity.vo.PurchaseDetailWithKeyVo;
import com.guli.mall.wms.entity.vo.PurchaseDetailWithSkuNameVo;
import com.guli.mall.wms.feign.ProductServiceFeignClient;
import com.guli.mall.wms.service.WareInfoService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;

import com.guli.mall.wms.mapper.PurchaseDetailMapper;
import com.guli.mall.wms.entity.PurchaseDetailEntity;
import com.guli.mall.wms.service.PurchaseDetailService;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailMapper, PurchaseDetailEntity> implements PurchaseDetailService {


    @Resource
    ProductServiceFeignClient productServiceFeignClient;

    @Resource
    WareInfoService wareInfoService;

    @Override
    public PageUtils<PurchaseDetailWithSkuNameVo> queryPage(Integer currentPage, Integer size, PurchaseDetailWithKeyVo purchaseDetailWithKeyVo) {

        Page<PurchaseDetailEntity> page = new Page<>(currentPage, size);
/*
        key: null,
        skuId: null,
        wareId: null,
        status: null,
* */
        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();

        if (!ObjectUtils.isEmpty(purchaseDetailWithKeyVo.getSkuId())) {
            wrapper.eq("sku_id", purchaseDetailWithKeyVo.getSkuId());
        }

        if (!ObjectUtils.isEmpty(purchaseDetailWithKeyVo.getWareId())) {
            wrapper.eq("ware_id", purchaseDetailWithKeyVo.getWareId());
        }

        if (!ObjectUtils.isEmpty(purchaseDetailWithKeyVo.getStatus())) {
            wrapper.eq("status", purchaseDetailWithKeyVo.getStatus());
        }

        if (!ObjectUtils.isEmpty(purchaseDetailWithKeyVo.getKey())) {
            if (Pattern.matches("^[0-9]+$", purchaseDetailWithKeyVo.getKey())) {
                wrapper.and(w -> {
                    w.eq("purchase_id", purchaseDetailWithKeyVo.getKey())
                            .or().
                            eq("id", purchaseDetailWithKeyVo.getKey());
                });
            }
        }

        Page<PurchaseDetailEntity> resultPage = this.page(page, wrapper);

        Page<PurchaseDetailWithSkuNameVo> copy = CopyPageProperties.copy(resultPage, PurchaseDetailWithSkuNameVo.class);


        for (PurchaseDetailWithSkuNameVo purchaseVo : copy.getRecords()) {
            if (!ObjectUtils.isEmpty(purchaseVo.getSkuId())) {

                Map<String, String> skuMap = productServiceFeignClient.getInfoAndReturnTo(purchaseVo.getSkuId());

                purchaseVo.setSkuName(skuMap.get("skuName"));
            }

            if (!ObjectUtils.isEmpty(purchaseVo.getWareId())) {
                WareInfoEntity wareInfoEntity = wareInfoService.getById(purchaseVo.getWareId());

                if (!ObjectUtils.isEmpty(wareInfoEntity)) {
                    purchaseVo.setWareName(wareInfoEntity.getName());
                }
            }
        }

        return new PageUtils<>(copy);
    }

    @Override
    public List<PurchaseDetailEntity> getPurchaseDetailByPurchaseId(Long purchaseId) {

        QueryWrapper<PurchaseDetailEntity> wrapper = new QueryWrapper<>();

        wrapper.eq("purchase_id",purchaseId);

        return this.list(wrapper);
    }

}